package com.originskyseed.platform.user.controller;

import java.util.Arrays;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.originskyseed.platform.common.api.util.PasswordUtil;
import com.originskyseed.platform.core.aspect.annotation.RequiredParams;
import com.originskyseed.platform.core.base.BaseController;
import com.originskyseed.platform.core.base.BaseLocaleResource;
import com.originskyseed.platform.core.base.BasePage;
import com.originskyseed.platform.core.base.BaseResponseBody;
import com.originskyseed.platform.core.common.code.CommonResponseCode;
import com.originskyseed.platform.core.filter.helper.SingleSignOnTokenHelper;
import com.originskyseed.platform.core.util.bean.PrimaryKeyGeneratorUtil;
import com.originskyseed.platform.user.i18n.RegisterResponseMsg;
import com.originskyseed.platform.user.i18n.UserResponseMsg;
import com.originskyseed.platform.user.model.ChannelSub;
import com.originskyseed.platform.user.model.UserInfo;
import com.originskyseed.platform.user.service.IChannelSubService;
import com.originskyseed.platform.user.vo.request.ChannelSubRequestVO;

/**
 * 子渠道信息表Controller类（OSI_CHANNEL_SUB）
 * 
 * @author WuZhuoXi
 */
@Controller
@RequestMapping("/channelSub")
@BaseLocaleResource(localeFileName = "i18n_user")
public class ChannelSubController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(ChannelSubController.class);

	@DubboReference
	private IChannelSubService channelSubService;

	/**
	 * 分页查询
	 * 
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/channelSubList.json", method = RequestMethod.POST)
	@RequiredParams(names = { "pageNo", "pageSize" })
	public BaseResponseBody<BasePage<ChannelSub>> channelSubList(HttpServletRequest request) {
		BaseResponseBody<BasePage<ChannelSub>> result = null;
		try {
			// 请求对象
			ChannelSubRequestVO requestVO = this.getParameterBean(ChannelSubRequestVO.class);
			UserInfo userInfo = SingleSignOnTokenHelper.getCurrentUserInfo(UserInfo.class);
			requestVO.setCreatorId(userInfo.getId());

			// 分页查询
			BasePage<ChannelSub> page = new BasePage<ChannelSub>(requestVO.getPageNo(), requestVO.getPageSize());
			page = this.channelSubService.selectListByPage(requestVO.toMap(), page);
			result = new BaseResponseBody<BasePage<ChannelSub>>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_LIST_SUCCESS.getMsg()), page);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<BasePage<ChannelSub>>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_LIST_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 保存信息
	 * 
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/channelSubSave.json", method = RequestMethod.POST)
	@RequiredParams(names = {})
	public BaseResponseBody<ChannelSub> channelSubSave(HttpServletRequest request) {
		BaseResponseBody<ChannelSub> result = null;
		try {
			// 请求对象
			ChannelSub channelSub = this.getParameterBean(ChannelSub.class);

			if (StringUtils.isNotBlank(channelSub.getId())) {
				this.channelSubService.updateByPrimaryKeySelective(channelSub);
			} else {
				// 判断密码
				if (channelSub.getPassword().length() < 6) {
					return new BaseResponseBody(false, CommonResponseCode.ERROR.getCode(), "密码长度不能小于六位数", false);
				} else if (channelSub.getPassword().length() > 15) {
					return new BaseResponseBody(false, CommonResponseCode.ERROR.getCode(), "密码长度不能大于十五位数", false);
				} else if (!channelSub.getPassword().matches("^(?=.*[0-9])(?=.*[a-zA-Z])[0-9A-Za-z!@#$%^&*,\\.]{6,15}$")) {
					return new BaseResponseBody(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), RegisterResponseMsg.PASSWORD_SIMPLE_ERROR.getMsg()), false);
				}
				UserInfo userInfo = SingleSignOnTokenHelper.getCurrentUserInfo(UserInfo.class);
				channelSub.setCreator(userInfo.getName());
				channelSub.setCreatorId(userInfo.getId());
				channelSub.setPassword(DigestUtils.md5DigestAsHex(channelSub.getPassword().getBytes()));
				channelSub.setChannelSubId(PrimaryKeyGeneratorUtil.getInstance().getRandomKey());
				this.channelSubService.insertSelective(channelSub);
			}
			result = new BaseResponseBody<ChannelSub>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_SAVE_SUCCESS.getMsg()), channelSub);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<ChannelSub>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_SAVE_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 删除信息
	 * 
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/channelSubDelete.json", method = RequestMethod.POST)
	@RequiredParams(names = { "ids" })
	public BaseResponseBody<Boolean> channelSubDelete(HttpServletRequest request) {
		BaseResponseBody<Boolean> result = null;
		try {
			// 请求参数
			String[] ids = this.getParameterArray("ids", new String[] {});

			this.channelSubService.deleteByPrimaryKeys(Arrays.asList(ids));
			result = new BaseResponseBody<Boolean>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_DELETE_SUCCESS.getMsg()), true);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<Boolean>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_DELETE_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 重置密码
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/resetPassword.json", method = RequestMethod.POST)
	@RequiredParams(names = { "id" })
	public BaseResponseBody<String> resetPassword(HttpServletRequest request) {
		BaseResponseBody<String> result = null;
		try {
			// 请求对象
			ChannelSub channelSub = this.getParameterBean(ChannelSub.class);
			String password = PasswordUtil.getStringRandom(6);
			channelSub.setPassword(password);
			this.channelSubService.updateByPrimaryKeySelective(channelSub);
			result = new BaseResponseBody<String>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_SAVE_SUCCESS.getMsg()), password);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<String>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_SAVE_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}
}